home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1454.dms / var1454.adf / Play / Play.c < prev    next >
C/C++ Source or Header  |  1992-05-07  |  7KB  |  238 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Sound                   Amiga C Club       */
  7. /* Chapter: EasySound                   Tulevagen 22       */
  8. /* File:    Play.c                      181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-07                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program can be either used from Workbench or the CLI. */
  21. /* If run from the CLI you give it one or more file names     */
  22. /* containing sampled sound data, and this program will play  */
  23. /* all the sound effects after each other.                    */
  24. /*                                                            */
  25. /* This program can also be used from Workbench. The user can */
  26. /* now simply duble click on the icons he/she wants to play.  */
  27. /* Note that these icons must have been given this program's  */
  28. /* name as default tool name, or else they do not know which  */
  29. /* program should be started when double clicked on. You can  */
  30. /* set the default tool name with help of the "Info" program  */
  31. /* which can be started from the Workbench menus. Give the    */
  32. /* field "Default Tool" the string "SYS:Utilities/Play" if    */
  33. /* you have placed this program in the "Utilities" directory  */
  34. /* on your system disk.                                       */
  35. /*                                                            */
  36. /* This program can also be used to play sound files which    */
  37. /* have not been prepared with a default tool name. The user  */
  38. /* simply clicks once on this program's icon, and while       */
  39. /* holding one of the SHIFT keys pressed clicks once on each  */
  40. /* sound file he/she want to play. When all files have been   */
  41. /* selected he/she simply double clicks on the last icon.     */
  42. /* This program will then automatically be started and the    */
  43. /* selected files played.                                     */
  44. /*                                                            */
  45. /* This program is a good example of how to precess the       */
  46. /* Workbench arguments.                                       */
  47.  
  48.  
  49.  
  50. /* This program must be linked with the EasySound module. */
  51.  
  52.  
  53. /* Include some important header files: */
  54. #include <exec/types.h>        /* Declares CPTR, BOOL and STRPTR. */
  55. #include <libraries/dos.h>     /* File lock etc... */
  56. #include <workbench/startup.h> /* Can be used from Workbench. */
  57. #include "EasySound.h"         /* Declares LEFT0, LEFT1, RIGHT0, etc. */
  58.  
  59.  
  60. /* Pointers to the sound effect: */
  61. struct SoundInfo *sound;
  62.  
  63.  
  64.  
  65. /* External global pointer to the Workbench data: */
  66. /* (Is automatically declared and defined in the  */
  67. /* start up module.)                              */
  68. extern struct WBStartup *WBenchMsg;
  69.  
  70.  
  71. /* Declare the function(s) in this module: */
  72. void main
  73. (
  74.   int argc,
  75.   char *argv[]
  76. );
  77.  
  78.  
  79. void main
  80. (
  81.   int argc,
  82.   char *argv[]
  83. )
  84. {
  85.   /* Counter: */
  86.   int counter;
  87.  
  88.   /* Number of arguments: */
  89.   int num_arg;
  90.  
  91.   /* Pointer to the workbench arguments: */
  92.   struct WBArg *our_wbarg;
  93.  
  94.   /* From Workbench? */
  95.   BOOL from_workbench;
  96.  
  97.   /* Temporary file name pointer: */
  98.   STRPTR file_name;
  99.  
  100.   /* Pointer to the "old" directory (lock):  */
  101.   struct FileLock *old_dir = NULL;
  102.   
  103.  
  104.  
  105.   /* Started from Workbench or CLI? */
  106.   if( argc == 0 )
  107.   {
  108.     /* Program started from Workbench! */
  109.  
  110.     /* Get a pointer to the fisrt argument: */
  111.     our_wbarg = WBenchMsg->sm_ArgList;
  112.  
  113.     /* Set number of arguments: */
  114.     num_arg = WBenchMsg->sm_NumArgs;
  115.  
  116.     /* From Workbench: */
  117.     from_workbench = TRUE;
  118.   }
  119.   else
  120.   {
  121.     /* Started from CLI! */
  122.     
  123.     /* Set the number of arguments: (Makes it */
  124.     /* easier since we now only have to check */
  125.     /* one variable.)                         */
  126.     num_arg = argc;
  127.  
  128.     /* From CLI: */
  129.     from_workbench = FALSE;
  130.   }
  131.  
  132.  
  133.  
  134.   /* If there is only one arguemnt the program has  */
  135.   /* been started without any selected sound files. */
  136.   /* Tell the user how to use this program:         */
  137.   if( num_arg == 1 )
  138.   {
  139.     /* Started from Workbench or the CLI? */
  140.     if( from_workbench )
  141.     {
  142.       /* Started from workbench. Tell the user how to */
  143.       /* use this program from Workbench:             */
  144.       printf( "Instruction:\n1. Click once on this icon\n" );
  145.       printf( "2. Press any of the shift keys and\n   hold it down.\n" );
  146.       printf( "3. Click once on each file to play.\n" );
  147.       printf( "4. Double click on the last icon.\n" );
  148.  
  149.       /* Wait fifteen seconds before we quit. The small */
  150.       /* window will be immediately closed when our     */
  151.       /* program terminates, and we must therefore give */
  152.       /* the user some time to read the instructions:   */
  153.       Delay( 15 * 50 );
  154.     }
  155.     else
  156.     {
  157.       /* Started from CLI. Tell the user how to */
  158.       /* use this program from CLI:             */
  159.       printf( "Usage: Play <sound 1> <sound 2> ...\n" );
  160.     }
  161.   }
  162.  
  163.  
  164.   
  165.   /* Set counter: */
  166.   counter = 1;
  167.   
  168.   /* As long as there are arguments left */
  169.   /* we stay in the while loop:          */
  170.   while( counter < num_arg )
  171.   {
  172.     if( from_workbench )
  173.     {
  174.       /* Program started from Workbench! */
  175.  
  176.       /* Go to next argument: (We skip the first argument since */
  177.       /* it is contains the program name, which we definitly    */
  178.       /* should not try to play!)                               */
  179.       our_wbarg++;
  180.  
  181.       /* Get one argument: */
  182.       file_name = our_wbarg->wa_Name;
  183.  
  184.       /* Change directory to where the sound file is: */
  185.       /* (Note that we have to save the old "lock" so */
  186.       /* we later can restore the current directory.) */
  187.       old_dir = (struct FileLock *)
  188.         CurrentDir( our_wbarg->wa_Lock );
  189.     }
  190.     else
  191.     {
  192.       /* Started from CLI! */
  193.       file_name = argv[ counter ];
  194.     }
  195.     
  196.     
  197.     
  198.     /* Prepare the sound: */
  199.     sound = ESPrepareSound( file_name );
  200.  
  201.     /* OK? */
  202.     if( sound )
  203.     {
  204.       /* Sound successfully prepared! */
  205.       printf( "Playing \"%s\"!\n", file_name );
  206.  
  207.       /* Lets play it: */
  208.       ESPlaySound( sound, MAXVOLUME, RIGHT1, SOUND_PRI_EFFECT,
  209.          NORMALRATE, ONCE, 0, 0, WAIT );
  210.  
  211.       /* Make sure that the sound effect has stopped:  */
  212.       ESStopSound( RIGHT1 );
  213.  
  214.       /* Remove the sound:*/
  215.       ESRemoveSound( sound );
  216.     }
  217.     else
  218.     {
  219.       /* ERROR! */
  220.       printf( "Could not load sound \"%s\"!\n", file_name );
  221.     }
  222.     
  223.  
  224.  
  225.     /* If we have changed current directory, restore it: */
  226.     if( old_dir )
  227.       CurrentDir( old_dir );
  228.  
  229.     /* Increase counter: */
  230.     counter++;
  231.   }
  232.  
  233.  
  234.  
  235.   /* The End! */
  236.   exit( 0 );
  237. }
  238.